package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.service.ISmsHomeBrandService;
import com.itheima.sms.entity.SmsHomeBrand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/brand")
public class SmsHomeBrandController {

    @Autowired
    private ISmsHomeBrandService smsHomeBrandService;

    /**
     * 添加首页推荐品牌
     *
     * @param list
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody List<SmsHomeBrand> homeBrandList) {
        smsHomeBrandService.saveBatch(homeBrandList);
        return AjaxResult.success();
    }

    /**
     * 批量删除推荐品牌
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam List<Long> ids) {
        smsHomeBrandService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 分页查询推荐品牌
     *
     * @param brandName
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "brandName", required = false) String brandName,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize,
                           @RequestParam(value = "recommendStatus", required = false) Integer recommendStatus) {
        Page<SmsHomeBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeBrand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(brandName != null, SmsHomeBrand::getBrandName, brandName)
                .eq(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus)
                .orderByDesc(SmsHomeBrand::getSort);

        smsHomeBrandService.page(page, lqw);
        PageInfo pageInfo = PageInfo.toPageList(page);

        return AjaxResult.success(pageInfo);

    }

    /**
     * 批量修改推荐品牌状态
     *
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public AjaxResult updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam("recommendStatus") String recommendStatus) {
        LambdaUpdateWrapper<SmsHomeBrand> luw = new LambdaUpdateWrapper<>();
        luw.in(SmsHomeBrand::getId, ids)
                .set(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus);
        smsHomeBrandService.update(luw);
        return AjaxResult.success();
    }

    /**
     * 修改推荐品牌排序
     *
     * @param id
     * @param map
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public AjaxResult updateSort(@PathVariable Long id, @RequestParam("sort") Integer sort, @RequestParam("id") Integer ids) {
        LambdaUpdateWrapper<SmsHomeBrand> luw = new LambdaUpdateWrapper<>();
        luw.eq(SmsHomeBrand::getId, id)
                .set(SmsHomeBrand::getSort, sort);

        smsHomeBrandService.update(luw);
        return AjaxResult.success();
    }
}
